package com.littora.alarm.utils;

import java.awt.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.Scanner;

public class FileUtils {
    public static String readLastLine(String filePath) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(filePath, "r");
        long len = raf.length();
        String lastLine = "";
        if (len != 0L) {
            long pos = len - 1;
            while (pos > 0) {
                pos--;
                raf.seek(pos);
                if (raf.readByte() == '\n') {
                    lastLine = raf.readLine();
                    break;
                }
            }
        }
        raf.close();
        return lastLine;
    }

    public static String findKeywork(String filePath,String keywork) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(filePath, "r");
        long len = raf.length();
        if (len != 0L) {
            long pos = len - 1;
            while (pos > 0) {
                pos--;
                raf.seek(pos);
                if (raf.readByte() == '\n') {
                    String line = subLast(raf.readLine(),keywork);
                    if(line == null) {
                        pos--;
                        continue;
                    }
                    raf.close();
                    return line;
                }
            }
        }
        raf.close();
        return "";
    }

    public static String subLast(String content,String keyword) {
        if(content == null && !"".equals(content)) {
            return content;
        }
        int lastIndex = content.lastIndexOf(keyword);
        if(lastIndex == -1) {
            return null;
        }
        return content.substring(lastIndex);
    }

    public static void cleanLog(String fileDir) {
        try {
            Path file = Paths.get(fileDir);
            Files.walkFileTree(file,
                    new SimpleFileVisitor<Path>() {
                        // 先去遍历删除文件
                        @Override
                        public FileVisitResult visitFile(Path file,
                                                         BasicFileAttributes attrs) throws IOException {
                            if(file.toString().indexOf(".log.0") != -1) {
                                Files.delete(file);
                            }
                            return FileVisitResult.CONTINUE;
                        }
                    }
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean isProcessRunning(String processName) throws IOException {
        Runtime runtime = Runtime.getRuntime();

        try {
            Process process = runtime.exec("cmd /c Tasklist");
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String s;
            while ((s = in.readLine()) != null) {
                s = s.toLowerCase();
                if (s.startsWith(processName)) {
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        System.err.println(FileUtils.subLast("tcp://server.natappfree.cc:40581","tcp://"));
    }
}
